
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections.Generic;
using System.Text;

using Swaf;
using Swaf.BizObj;
using Swaf.Gui;
using Swaf.Security;

namespace Reservations.Gui
{
	public class ReservationWizardBizProc : BaseBusinessProcess
	{
		public ReservationWizardBizProc(ActiveBizProc procState) : base(procState)
		{
			string startRes = (string)procState.get("startResImmediately");
			if (App.sessionData["CheckedAutoStartRes"] == null && (string.Compare("true", startRes, false) == 0 ||
				m_procState.BPC.HostView.ViewName.EndsWith(startRes)) &&
				(!(App.sessionData.get("startedRes") is bool ||
				(App.sessionData.get("startedRes") is bool && !(bool)App.sessionData.get("startedRes")))))
				makeReservation();
			App.sessionData["CheckedAutoStartRes"] = true;
		}
		
		protected override void setupRuleNames()
		{
		}

		protected override Privileges CurrentEditPrivilege
		{
			get { return Privileges.Delete; }
		}

		public void makeReservation()
		{
			App.ruleMgr.execute("initiateNewReservation");
			App.sessionData.put("startedRes", true);
			App.sessionData.put("StartCheckoutWizard", "true");
			ActiveBizProc checkout = m_procState.getParentBizProc("info");
			if (checkout != null)
				checkout.BizProc.Actions["StartWizard"].trigger();
		}

		//
		protected override bool validateBizObj(IBizObj obj)
		{
			IResults rc = App.ruleMgr.execute("validateReservation", obj);
			List<UserMessage> msgs = rc.CallResults as List<UserMessage>;
			if (msgs == null)
				return true;
			m_procState.BPC.Messenger.addRange(msgs);
	
			//Try to stop current wizard step from proceeding while remaing with invalid reservation
			IBizObj wizardInfo = m_procState.get("info",false) as IBizObj;
			if (wizardInfo != null)
				wizardInfo.put("stepCompleted", false);

			return false;
		}

		public override IResults updateBizObj(IBizObj obj)
		{
			IResults rc = null;
			IBizObj wizardInfo = m_procState.get("info", false) as IBizObj;
			if (wizardInfo != null && !(bool)obj.get("hasReviewedResPolicy") && (bool)wizardInfo.get("inCheckout"))
			{
				wizardInfo.put("stepCompleted", false);
				m_procState.BPC.Messenger.add("Error-RequiredFieldMissing", null);
			}
			
			return rc;
		}

		protected override void setupActions()
		{
			base.setupActions();
			Actions.addAction(new GenericAction("MakeReservation", true, makeReservation, this));
		}

		public override void updateActionState()
		{
			base.updateActionState();

			//((GenericAction)Actions["MakeReservation"]).Availability = CanEdit ? ActionAvailability.enabled : ActionAvailability.none;
		}
	
	}
}
